﻿/**************************************************************************************************\
++++++++++++++++++++++++++++++ ARKADIUM INTELLECTUAL PROPERTY TERMS ++++++++++++++++++++++++++++++
The Software is protected by US copyright laws and international treaties. Licensee must treat the 
Software like any other copyrighted material – for example a book. Licensee acknowledges that 
Arkadium owns the Software and that nothing set forth herein conveys to Licensee any ownership in 
or to the Software, including without any Derivative Works of the Software.  
++++++++++++++++++++++++++++++ ARKADIUM INTELLECTUAL PROPERTY TERMS ++++++++++++++++++++++++++++++

AUTHOR: Neil Mrva

PURPOSE: This class provides functionality that calculates the current Frames Per Second that a SWF
is running at. This class should be linked to a movieclip on stage that provides a dynamic textfield
to write the number of frames to. Using this movieclip's onEnterFrame is the method of calculating 
the FPS during runtime and is currently set to update every 250 milliseconds.
\**************************************************************************************************/

class arkadium.FPSMeter extends MovieClip
{
	private var _sdtFrames:MovieClip; // Assumed textfield contained in this linked movieclip
	
	private var _nStartTime:Number;
	private var _nElapsedFrames:Number;
	
	// These members exist here ONLY for optimization so that the onEnterFrame function doesn't create 
	// them locally each time. 
	private var _nCurrentTime:Number;
	private var _nElapsedTime:Number;
	
	/**************************************************************************************************\
	FPSMeter - Constructor used set up the timing variables.
	\**************************************************************************************************/
	private function FPSMeter()
	{
		// Let's get the start time of when the movie begins.
		_nStartTime = getTimer();
		
		// Since no frames have passed, we'll set this to zero.
		_nElapsedFrames = 0;
		
		// These variables could be local in the update function but for optimization reasons, we initialize
		// them now.
		_nCurrentTime = _nElapsedTime = null;
	}
	
	/**************************************************************************************************\
	onEnterFrame - This essentially is our update function that calculates the frames per second. Read
	the comments contained within to understand how it works.
	\**************************************************************************************************/
	private function onEnterFrame()
	{
		// We must capture the exact time that this onEnterFrame function is executed.
		_nCurrentTime = getTimer();
		
		// We determine the total time that has elapsed since the last time we update the meter by taking 
		// the current time and finding the difference from the start time ( or time of last update ).
		_nElapsedTime = _nCurrentTime - _nStartTime;
		
		// Becuase we don't update the meter every onEnterFrame, we keep a running total of all the frames 
		// that passed so we can calculate the average time per frame when an update occurs.
		_nElapsedFrames++;
		
		// If the total elapsed time is equal to or over the update time ( in milliseconds ), we calculate
		// frames per second and update the meter. In this case 250 milliseconds means we're updating the 
		// meter 4 times a second.
		if ( _nElapsedTime >= 250 )
		{
			// In order to correctly calcualte the FPS - we must first divide the total elapsed time it took
			// from the last meter update by the number of onEnterFrames that passed. That will result in an 
			// average time ( in milliseconds ) that each frame took to get here. We then divide 1000
			// ( 1000 milliseconds = 1 second ) by the average of the frames' time in order to determine how 
			// many frames are taking place in one second.
			_sdtFrames.text = Math.round( 1000 / ( _nElapsedTime / _nElapsedFrames ) ).toString();
			// For Debugging
			//trace( "ET: " + nElapsedTime + " EF: " + _nElapsedFrames + " FPS: " + _sdtFrames.text );

			// Because we just updated the meter, we set the new update time to the current time of this frame 
			// and reset the number of frames that passed.
			_nStartTime = _nCurrentTime;
			_nElapsedFrames = 0;
		}
	}
	
}